import { ref } from 'vue'
import { adapter, request } from '@/request'
import { priceSheetByPost } from '@/core/api/priceSheet'
import { message } from 'ant-design-vue'
import is from '@/utils/is'
import type { TypeEnum, VersionTypeEnum } from '@/core/enums/type'
import type { ApproveStatusEnum } from '@/core/enums/status'
import type { ProductEnum } from '@/core/enums/platform'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PriceKeyword } from '@/core/interfaces/keyword'
import type { ProxyPack, SellerPack } from '@/core/interfaces/base'
import type { Combo, Price } from '@/core/interfaces/combo'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import { useMainStore } from '@/store/useMainStore'
export interface PriceSheetCreator extends SellerPack, ProxyPack, Combo {
  type?: TypeEnum
  productId: ProductEnum
  priceAskWords: PriceKeyword[]
  oldOrderId?: PriceSheet['oldOrderId']
  approveStatus: ApproveStatusEnum
}

export interface CiBaoChannelPrice extends PriceSheetCreator, Price {
  productId: ProductEnum
  type: TypeEnum
  versionType: VersionTypeEnum
  areas: '全国'
  machineExamine?: number
  zzbwOrderNo: string
  oldCustomerAllWebOrderNo?: string
  platformIds?: string
  // 新增字段
  premiumPrice: Price['premiumPrice']
  premiumRatio: Price['premiumRatio']
  marketPrice: Price['price']
}

export default function createPriceSheet() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)
  const response = ref<any>()

  const fetch = (data: Partial<CiBaoChannelPrice>) => {
    const { url, method } = priceSheetByPost()
    loading.value = true
    const mainStore = useMainStore()
    let mini = 0
    let zzosid = ''
    console.log(data.productId, '提交版本')

    if (data.productId == 7 || data.productId == 10) {
      if (data.comboId == 15 || data.comboId == 16) {
        mini = 0
      } else {
        mini = 50
      }
    } else {
      mini = 0
    }
    // mini = 0
    if (data.productId == 9) {
      zzosid = mainStore.zzbwOrderNod
    }
    return service({
      config: {
        url,
        method,
        data: {
          ...data,
          mini: mini
          // zzbwOrderNo: zzosid
          // mainStore.zzbwOrderNo: zzosid
        }
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        if (is.error(err)) {
          message.error(err.message)
        } else {
          message.error('请求失败，请稍候再试...')
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
